package com.ruoyi.web.controller.byw;

import java.math.BigDecimal;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.system.domain.BywEmployee;
import com.ruoyi.system.domain.BywEmployeeBillingDetails;
import com.ruoyi.system.domain.BywStage;
import com.ruoyi.system.service.IBywEmployeeService;
import com.ruoyi.system.service.IBywStageService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.BywTalentBillingDetails;
import com.ruoyi.system.service.IBywTalentBillingDetailsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 达人结算Controller
 * 
 * @author ruoyi
 * @date 2024-05-25
 */
@Api("达人结算")
@RestController
@RequestMapping("/byw/talent-billing-details")
public class BywTalentBillingDetailsController extends BaseController
{
    @Autowired
    private IBywTalentBillingDetailsService bywTalentBillingDetailsService;

    @Autowired
    private IBywStageService stageService;
    /**
     * 查询达人结算列表
     */
    @ApiOperation("查询结算记录")
    @PreAuthorize("@ss.hasPermi('byw:talent-billing-details:list')")
    @GetMapping("/list")
    public TableDataInfo list(BywTalentBillingDetails bywTalentBillingDetails)
    {
        startPage();
        List<BywTalentBillingDetails> list = bywTalentBillingDetailsService.selectBywTalentBillingDetailsList(bywTalentBillingDetails);
        return getDataTable(list);
    }

    /**
     * 导出达人结算列表
     */
    @PreAuthorize("@ss.hasPermi('byw:talent-billing-details:export')")
    @Log(title = "达人结算", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, BywTalentBillingDetails bywTalentBillingDetails)
    {
        List<BywTalentBillingDetails> list = bywTalentBillingDetailsService.selectBywTalentBillingDetailsList(bywTalentBillingDetails);
        ExcelUtil<BywTalentBillingDetails> util = new ExcelUtil<BywTalentBillingDetails>(BywTalentBillingDetails.class);
        util.exportExcel(response, list, "达人结算数据");
    }

    /**
     * 获取达人结算详细信息
     */
    @PreAuthorize("@ss.hasPermi('byw:talent-billing-details:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(bywTalentBillingDetailsService.selectBywTalentBillingDetailsById(id));
    }

    /**
     * 新增员工结算
     */
    @ApiOperation("达人新增结算")
    @PreAuthorize("@ss.hasPermi('byw:employee-billing-details:add')")
    @Log(title = "达人结算", businessType = BusinessType.INSERT)
    @PostMapping
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult add(@RequestBody BywTalentBillingDetails bywTalentBillingDetails)
    {
        BywStage bywStage = stageService.selectBywStageByStageId(bywTalentBillingDetails.getTalentId());
        if (bywStage == null){
            return error("当前达人不存在");
        }
        if((bywStage.getUnlockedNum() - bywStage.getSettledNum()) < bywTalentBillingDetails.getVitaeNum()){
            return error("当前可结算简历数不足");
        }

        bywStage.setSettledNum(bywStage.getSettledNum() + bywTalentBillingDetails.getVitaeNum());
        bywStage.setSettledMoney(bywStage.getSettledMoney().add(new BigDecimal(bywTalentBillingDetails.getMoney())));
        bywStage.setToSettleMoney(bywStage.getToSettleMoney().subtract(new BigDecimal(bywTalentBillingDetails.getMoney())));
        bywStage.setUpdateBy(getUsername());
        bywStage.setUpdateTime(DateUtils.getNowDate());
        stageService.updateBywStage(bywStage);

        bywTalentBillingDetails.setCreateBy(getUsername());
        return toAjax(bywTalentBillingDetailsService.insertBywTalentBillingDetails(bywTalentBillingDetails));
    }

    /**
     * 修改达人结算
     */
    @PreAuthorize("@ss.hasPermi('byw:talent-billing-details:edit')")
    @Log(title = "达人结算", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody BywTalentBillingDetails bywTalentBillingDetails)
    {
        return toAjax(bywTalentBillingDetailsService.updateBywTalentBillingDetails(bywTalentBillingDetails));
    }

    /**
     * 删除达人结算
     */
    @PreAuthorize("@ss.hasPermi('byw:talent-billing-details:remove')")
    @Log(title = "达人结算", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(bywTalentBillingDetailsService.deleteBywTalentBillingDetailsByIds(ids));
    }
}
